package com.example.springdemo;

import cn.gov.zcy.open.sdk.http.ResponseResult;
import cn.gov.zcy.open.sdk.open.HttpMethod;
import cn.gov.zcy.open.sdk.open.ZcyClient;
import cn.gov.zcy.open.sdk.open.ZcyOpenRequest;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@Slf4j
@SpringBootTest
public class JwtDemo {
//    private static final Logger log = LoggerFactory.getLogger(JwtDemo.class);
    private static ZcyClient zcyOpenClient = new ZcyClient();
    private static final String API_GATEWAY_SANDBOX = "http://sandbox.zcygov.cn/";
    private static final String API_GATEWAY_ONLINE = "http://api.zcygov.cn/";
    private static final String APP_KEY = "248010143";
    public static final String pkcs8PrivateKey = "-----BEGIN PRIVATE KEY-----\n" +
            "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALu5FfydHzwGgcZY\n" +
            "dz9A6P7FYWqPoUcBj7WKxEs0gz2D8U9YZ9x/JL8Ot2hej8ZBGyXU0J3PShB+3WoA\n" +
            "M/ieFdvLj9bLnfmUfOPXmMdl69daQJ7iyNTMPZc8Ebsoc74oYUoBABF1oKhRPcwZ\n" +
            "az1sc/aUSegpxGHYqpM6jCJSwWMxAgMBAAECgYEAloAW7oYRipA07w3x3JZvtgIM\n" +
            "zeAFDuZjjsqIATtClMj5aAGakx6kGUD1LgnR/sInv7bU4hBUctEjNl6fTA2Bu2lm\n" +
            "2447WWRPnx4Vrx6Sel268PaVjlgRe/eCEM7IIW73LGiKcfPJI0y5E8WtHnEw+a8c\n" +
            "gIagBVu3YhIaVQGuWLkCQQD3iuyphPF/+jbnwgn5qIRNg3u5AyIpL0A8i6gztdU7\n" +
            "m3/iwvDS1+hWfLYK/6GDvECqI6U0D+K2hG+Vg3X3CCKnAkEAwiL2U/7SCc5TVjsk\n" +
            "bAL3cxvraC8NPnGqjZmA0nSwIMvGl4uCscuXDYy54O2j42teBJ/lLqFZPOxp0WOn\n" +
            "LxE+ZwJAEUEFresuShVOrmi/+T7dueloPkURpH0+pPm3B6Os8TKtf0jHMGsMLrsS\n" +
            "lUGsKFp+x10krrRMwRwKCwnWtxlCEQJAeOs16pNpV9o/ZhJoWS5v2i6lrbrA0QgT\n" +
            "ZycZafxsR8Ek7PdkbEGW0EW68TKTRe92HANLyIOPBbdrIZSkaVigOQJBAKibJv1W\n" +
            "VWKvwLIPlo42OpCrDptsYwlpXVAmWaXcF7n4uS2TL46LF2Jp7/ErC1wuZ5jGm41E\n" +
            "pNaW91gHvmFTn/o=\n" +
            "-----END PRIVATE KEY-----";

    public JwtDemo() {
    }

    @Test
    public void doGetRequest() throws Exception {
        ZcyOpenRequest request = ZcyOpenRequest.buildJwtRequest(APP_KEY, "-----BEGIN PRIVATE KEY-----\n" +
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCxgOYT7ye8ZgcZ\n" +
                "z0E1vNh7zbHDCQ8C93lxOdpDCDUH5qMQ/MPTWGmwBn+0oHJSu+8ZDS5h8rcIq3G1\n" +
                "yERlfDkUe3/cqZlKLpxCChhRX+XoJAZ2dR6JhLFOWdQnEvJTXznU1/znH4FY1Zwh\n" +
                "CHjPubkY/ZjkkbPrriLsQAbEGwVjrsh1wdqM3hvGTu9QeKg/++gqzL555gpUHqJ8\n" +
                "B77DWqRS96pPPCR/EnyitKdP6+Ve0gmK7QAGQR0MDjRf0lWayX/u6B+KvKNCBabF\n" +
                "qY7wmhu7VypJ0XOrtM8SVv8kLnjTGf+md7e07t6VeI1CkV81Y4+kXr0v95nrz7b3\n" +
                "Ffbuo/sZAgMBAAECggEAIqK4QmOEKWrvL8YVP1seEupMKXZ3C2kmtUVIpZPQZT5H\n" +
                "y2THAVJbLRHXkXNNW394ZDcCOfKWbIanpYTyc6sNqEWA6ZneY2885lX58Mb5oZaF\n" +
                "/E9WKhdOUa+NiiJUTphiPHn4y1CGeMMVuAxjJoVi7NFvKAqWRQKxpc/IgjOTlaON\n" +
                "x1Tdmdl++QbgXRcbYFMCLpL2FoSzPjahEbX5OwCb5p00I7WZ8jXXUcuAYlBFksLO\n" +
                "5bKkrzAUfNHY8Hp+r8OeBW5lDQ3gGtOEjSGzAzX1DLS4wUmiATl5JTud6apqSL4h\n" +
                "qXGpVZBpcwIT4jTVaelM3hGdUWt03p3OeyBw6mNLQQKBgQDpqutv4SojMy8ArXj8\n" +
                "nl1jbM+/65dAze4y0mdlzVOrZEyYq58Q1kkLOdNq/Sh5VWPOD+bn1pdXcmpIXTDp\n" +
                "vsWBd0BS8uzrFW5v0T8zBVLv/BIAkj7ummX6HDQgSkaJfebJxcqr80enIRqIkcG+\n" +
                "uJIagmAnuj8TjgRUMJr1SdwHNQKBgQDCd9Os6yP6xCx5409BdpI3hKGyDfjAVljO\n" +
                "8fQMEcbGLiVJ1WKrPsWRm25Ix1bsLSLtt7CbT/+J7Ryj0OpObTnEytha5b715eRW\n" +
                "5EZ7h84sGB43nCOMxxa7XQR1htyQs3Co9DpSo24QEbln+mK7f3yFuJDkrGWFca35\n" +
                "8Difkz2M1QKBgQDeGfUhab9Yp+z0Uu8eBxUzTRkHDlK9aUARFIbtb8YosfRQZUiT\n" +
                "g2r4faVkG29eLPjpw5myIfnGR92YHORCOW+YCJaegYMKTFulcxoET/ouW8Vsv5WL\n" +
                "zDxECIzPaGo1g/NbNGLZICuqQz/qMckblGpjQitNd60+1A1vTKftoJ/ARQKBgFIE\n" +
                "6gE/d4BXZw7NgWrx0H+b7/rCC81LXc+CmtTQVo7VfaKjXictpXRhIuWNNasM6nNq\n" +
                "ixEmb3fDytWI0pnbVYcbqp7D0YOyv1iqlNObFqjgTzZTI2dHvN2vQKc1Byf8lqKb\n" +
                "x1F9XnlmkFU7dJLyFRntGydnUrYymRcWaztahZY1AoGBAMqWzFeEjXCgvQkhunhK\n" +
                "CfoV3UWpAE+kde3Vb8QDQofUg9+XIYGt719hYGHVPeVD+hstoNj4GkFJouiq/kZG\n" +
                "iDJ9LcYwuX5EmBCtlHcVfoow8YtqhzOsPdQQFRfN85y67j8K4VqLEuXxXWEIFPYf\n" +
                "RvWkc3GNHy5N9iSiIeezyBmj\n" +
                "-----END PRIVATE KEY-----", true, "https://convenient.wsjkw.zj.gov.cn");
        request.setUri("/test/open/api/zcy.vaccine.his.dog.additional.sync");
        request.setMethod(HttpMethod.GET);
        Map<String, String> jwtHeaders = new HashMap();
        jwtHeaders.put("version", "1.0.0");
        request.setHeaderMap(jwtHeaders);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "张三");
        jsonObject.put("age", 26);
        Map<String, Object> paramMaps = new HashMap();
        paramMaps.put("data", jsonObject.toString());
        request.setParamMap(paramMaps);
        ResponseResult result = zcyOpenClient.sendRequest(request);
        log.info(result.getBody());
    }

    @Test
    public void doPostRequest() throws Exception {
        ZcyOpenRequest request = ZcyOpenRequest.buildJwtRequest("142221113", "-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALuSx6cO06G1JU1G\nX5fAONlchygglpqueHaQIrkF+xCIzO5/2IVR/av1eWrHHgP9zwQf2TE4w3BGs+FD\ndTl3/RgEDKMxDO3Qlmp2qPMyQPVHwC/xTQ6hL7sM5Ex7UNBtuw6GF7OvfnX7yer5\ntFFJLSn9GF0fdbBxBopWyejeb9uPAgMBAAECgYEAs4fmjnAzgVhHTrIntrBl+S2F\ns5OVcpHBfQGyKlOeHRJCy5gUHm7+lIMMZnYUtY9NgE4XbPQ+o40YiGgvoATHeXo/\n21nfJEZDe+ASDWPj6PDWlCiFip5TAlIFacVx8o8bqXw4QXf50Uq5GVLqO2qcbaDv\nR83igqBO+Mz/qGEz9QECQQDzeQ5OOioxHOdudNQKxW843dVxhMAbyxqtG5Zen1I1\nK20SNnIY13CGLG8umnDpaincYqQDM+7/uLwtXy76SrYPAkEAxTlvIMo1m8tfHIDw\nl6HNPS3Pdxx1xCaeVmgVCnSotK1KTb4Lil75p4ruKxWCHJxUkvJK+7RAP+UOsHxU\nrsICgQJAbjmx9kxy6YUyaVlO3gmATvhvN1NoPaz3yMMikngCmbutR3+KrCBs+vGG\n7ExTLfk4NmQZ6LFMVE1oNApRlFLalQJAJWykncBBM/L/dVUyUlOtsnJC8ANChZoe\naZ8OfxFsAfzwIJhzcCpdjWFbmGlYaz1u/wNjHygxGO7KPssY9qByAQJAYAzz5hka\nY5Xp+IG9YrVSnMzi6oqH5yINr3/wnX0oIEkQ3+bnof6gAuL6hdGEYPoA9m72lgcf\nLxnzZ3gwYzeLng==\n-----END PRIVATE KEY-----\n", true, "http://sandbox.zcygov.cn/");
        request.setUri("/test/zcy.test.demo");
        request.setMethod(HttpMethod.POST);
        Map<String, String> jwtHeaders = new HashMap();
        jwtHeaders.put("version", "1.0.0");
        request.setHeaderMap(jwtHeaders);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("outerTradeNo", "6a20a4e2292c48abbfcd8b1e0746d403");
        jsonObject.put("seqNo", "1525685574372");
        JSONObject details = new JSONObject();
        details.put("payeeAccountName", "aaa");
        details.put("payeeAccountNo", "888888888");
        details.put("payeeBank", "aaa");
        details.put("payerAccountName", "aaa");
        details.put("payerBank", "aaa");
        details.put("payerBankNo", "201820182018201888");
        List<JSONObject> list = new ArrayList();
        list.add(details);
        jsonObject.put("details", list);
        Map<String, Object> paramMaps = new HashMap();
        paramMaps.put("data", jsonObject.toString());
        request.setParamMap(paramMaps);
        ResponseResult result = zcyOpenClient.sendRequest(request);
        log.info(result.getBody());
    }
}
